package com.zxj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seventh.common.pojo.R;
import com.zxj.mapper.CarMapper;
import com.zxj.pojo.entity.TbBrand;
import com.zxj.pojo.entity.TbCar;
import com.zxj.pojo.vo.CarVo;
import com.zxj.pojo.vo.OperVo;
import com.zxj.service.CarService;


import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

/**
 * @program: clms-fifth
 * @description:
 * @author: 张修杰
 * @create: 2021-07-23 15:01
 **/
@Service
public class CarServiceImpl implements CarService {
    @Resource
    CarMapper carMapper;
    @Override
    public R selectAllCar() {
        List<TbCar> tbCars =carMapper.selectList(null);
        return R.ok().put("AlltbCars",tbCars);
    }
    //vin码的唯一性校验
    @Override
    public R selectVin(TbCar tbCar) {
        QueryWrapper<TbCar> wrapper=new QueryWrapper<>();
        //System.out.println(tbCar);
        if (StringUtils.isNotEmpty(tbCar.getVin())){
            wrapper.eq("vin",tbCar.getVin());
        }
        if (tbCar.getId()!=null){
            wrapper.ne("id",tbCar.getId());
        }
        if (StringUtils.isNotEmpty(tbCar.getVin())){
            TbCar tbCar1=carMapper.selectOne(wrapper);
            if (tbCar1!=null){
                return R.error().message("已经存在");
            }else {
                return R.ok().message("成功");
            }
        }else {
            return R.error().message("不能为空");
        }
    }

    @Override
    public R selByShopId(CarVo vo) {
        QueryWrapper<TbCar> wrapper=new QueryWrapper<>();
        wrapper.eq("shop_id",vo.getShopId());
        wrapper.eq("state",1);
        if (vo.getPx()!=null){
            if (vo.getPx()==1){
                wrapper.orderByAsc("price");
            }else if (vo.getPx()==2){
                wrapper.orderByDesc("price");
            }
        }
        if (vo.getBrandId()!=null){
            wrapper.eq("brand_id",vo.getBrandId());
        }
        if (vo.getType()!=null){
            wrapper.eq("type",vo.getType());
        }
        List<TbCar> tbCars = carMapper.selectList(wrapper);
        return R.ok().put("allCarList",tbCars);
    }

    @Override
    public R findAllCarPage(Integer currentPage, Integer pageSize, OperVo operVo) {
        QueryWrapper<TbCar> wrapper=new QueryWrapper<>();

        if (StringUtils.isNotEmpty(operVo.getCarName())){
            wrapper.like("carName",operVo.getCarName());
        }
        if (StringUtils.isNotEmpty(operVo.getName())){
            wrapper.like("name",operVo.getName());
        }
        Page<TbCar> page=new Page<>(currentPage,pageSize);
        IPage<TbCar> page1=carMapper.SaveCarList(page,wrapper);
        //carMapper.selectPage(page,wrapper);
        return R.ok().put("records",page1.getRecords()).put("total",page1.getTotal());

    }



    @Override
    public R updateCarById(TbCar tbCar) {
        int i= carMapper.updateById(tbCar);
        return (i>0)? R.ok():R.error();
    }

    @Override
    public R selectCarById(String id) {
        TbCar tbCar=carMapper.selectById(id);
        return R.ok().put("tbCar",tbCar);
    }

    @Override
    public R insertCar(TbCar tbCar) {
        int i= carMapper.insert(tbCar);
        return (i>0)?R.ok():R.error();
    }

    @Override
    public R deleteCar(String id) {
       int i=carMapper.deleteById(id);
       return (i>0)?R.ok():R.error();
    }


}
